package com.ke.utils;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ArticlePayUtil {
    public static String pay(Map map){
        AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipaydev.com/gateway.do" , "2021000117657418", "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCQmBmsN9kw6Fdn31Puc8W/3DGv8yeJ/Orep0n2KTey1SQnzYUgxrVmFTr40EaVN2BmeX1OXz6iUvRRPhj+K7CuefhKB9z7NeSuVnDJSMcXjkGPY+8Df2jjRP1uBTxss9QqXlRAPtiF2IpHWl5NxwZ4ei1SFR2rPh49AgaO7P3kDBP8uN8aoLVgEozTaD3EGMH1ncgxp27MS10fuyQtCCW3R9ogo7qBRp5VVc0B5WG1cqD2fYDE2akGJXzDiHHJ6M4jrnl6gxE2uQbvQwcAZs+2Uc9Wd/oeYfStuvlg8YGpD7TxhU4rc/GsPx8VzvaOP7fl6gSLoEQOL2bLCNMUdxUBAgMBAAECggEAJxJClau9r56Ix9OsDVoRTGfGsWHGBF5oZOPVc4PtvhAkJfE+EP5A1CXWepdlCzscfHRFrODFvv0lcbKS9FN/+ZWRxwda6jLE6WsILQvSaIYTJxj84/gQsqDYzlrx2fm50f0NTRLuVTSPIPrFUsT99Z4mEvELxFMTvq6GcSaO8Ij+cg8USF5/l5SKIhSvXtNh6nwaLlprRm6kHaG+4FUnZsVYllRcLib5Oal8lJjh2lsK0PEGdVtsR4ewjjvXKJsQ/D0CFCtDepLow/WsAvlvk5yMfCWAPlqE/18v2tFkgTqV9nHqahLOHmmDIgBGyfm18vlpSkfUNGk1gbLvriVD8QKBgQDVRRcoeXZ/rOaGi2QGQXeAismGQCpYX67tOnzhW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G+AX5Z2s4JYZzKSmvHwmFziyxJZp6QmBZE1/gJlGvhhWk8/kQD9Rnp+i8O5IgOlVxt7LHcsuU/uK2qS6rICIylirp9MSNov2UGi8jfNjkB9uLKIy4Q9Wcdi4GLiEONRu+GCmrFGtXt2ruTzf7NA+g9re3Adv9yHwwIDAQAB", "RSA2");  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
        //同步回调,前台页面
        alipayRequest.setReturnUrl( "http://localhost:8080/#/articleOrder" );
        //异步回调
        alipayRequest.setNotifyUrl( "http://5u3zvk.natappfree.cc/articleOrder/returnMessage" ); //在公共参数中设置回跳和通知地址
        /*alipayRequest.setBizContent( "{"  +
                "    \"out_trade_no\":\"20150320010101001\","  +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
                "    \"total_amount\":88.88,"  +
                "    \"subject\":\"Iphone6 16G\","  +
                "    \"body\":\"Iphone6 16G\","  +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
                "    \"extend_params\":{"  +
                "    \"sys_service_provider_id\":\"2088511833207846\""  +
                "    }" +
                "  }" ); //填充业务参数*/
        Map mapPay = new HashMap();
        mapPay.put("out_trade_no",map.get("orderId"));
        mapPay.put("product_code","FAST_INSTANT_TRADE_PAY");
        mapPay.put("total_amount",map.get("price"));
        mapPay.put("subject","付费文章");
        mapPay.put("body","文章");

        String jsonString = JSONObject.toJSONString(mapPay);
        alipayRequest.setBizContent(jsonString);

        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        return form;
    }



    public static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();

        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();

        for (Map.Entry<String, String[]> entry : entrySet) {
            String name = entry.getKey();
            String[] values = entry.getValue();
            int valLen = values.length;

            if (valLen == 1) {
                retMap.put(name, values[0]);
            } else if (valLen > 1) {
                StringBuilder sb = new StringBuilder();
                for (String val : values) {
                    sb.append(",").append(val);
                }
                retMap.put(name, sb.toString().substring(1));
            } else {
                retMap.put(name, "");
            }
        }
        return retMap;
    }

}
